package com.spdd.mt.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spdd.mt.biz.entity.MtRechargeOrderPreEntity;
import com.spdd.mt.biz.mapper.MtRechargeOrderPreMapper;
import com.spdd.mt.biz.service.MtRechargeOrderPreService;
import com.spdd.mt.biz.vo.MtRechargeOrderPreVo;
import com.spdd.mt.common.base.BaseException;
import com.spdd.mt.common.base.impl.BaseServiceImpl;
import com.spdd.mt.common.util.ConvertUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * MtRechargeOrderPre 充值预下单表  服务实现类
 * </p>
 *
 * @author shanfeilong
 * @since 2025-04-02
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class MtRechargeOrderPreServiceImpl extends BaseServiceImpl<MtRechargeOrderPreMapper, MtRechargeOrderPreEntity> implements MtRechargeOrderPreService {

    private QueryWrapper<MtRechargeOrderPreEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<MtRechargeOrderPreEntity> wrapper = new QueryWrapper<>();

        String id = (String) params.get("id");
        wrapper.lambda().eq(StrUtil.isNotBlank(id), MtRechargeOrderPreEntity::getId, id);

        List<Long> ids = (List<Long>) params.get("ids");
        wrapper.lambda().in(CollectionUtil.isNotEmpty(ids), MtRechargeOrderPreEntity::getId, ids);

        // wrapper.lambda().eq(MtRechargeOrderPreEntity::getIsDelete, 0);

        return wrapper;
    }

    @Override
    public Page<MtRechargeOrderPreVo> page(Map<String, Object> map) {
        Page<MtRechargeOrderPreEntity> pageObj = this.getPage(map);
        Page<MtRechargeOrderPreEntity> pageList = this.page(pageObj, getWrapper(map));

        return convertTarget(pageList, MtRechargeOrderPreVo.class);
    }

    @Override
    public List<MtRechargeOrderPreVo> list(Map<String, Object> map) {

        List<MtRechargeOrderPreEntity> list = this.list(getWrapper(map));

        return convertTarget(list, MtRechargeOrderPreVo.class);
    }

    @Override
    public MtRechargeOrderPreVo get(Long id) {
        if (id == null) {
            return null;
        }
        MtRechargeOrderPreEntity entity = this.getById(id);

        return convertTarget(entity, MtRechargeOrderPreVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MtRechargeOrderPreVo dto) {
        MtRechargeOrderPreEntity entity = ConvertUtils.sourceToTarget(dto, MtRechargeOrderPreEntity.class);

        dto.setId(null);
        this.save(entity);

        dto.setId(entity.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MtRechargeOrderPreVo dto) {
        if (dto.getId() == null) {
            throw new BaseException("主键为空");
        }

        MtRechargeOrderPreEntity entity = ConvertUtils.sourceToTarget(dto, MtRechargeOrderPreEntity.class);
        this.updateById(entity);
    }

}
